//
//  CZWelcomeController.swift
//  weiBo
//
//  Created by apple on 16/5/14.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SDWebImage
class CZWelcomeController: UIViewController {
    
    //懒加载背景图片
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //懒加载头像
    private lazy var iconImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "avatar_default_big"))
        image.layer.cornerRadius = 45
        image.layer.masksToBounds = true
        //设置网络头像
        if let url = CZUserCountViewModel.sharedUserAccount.userAcount?.avatar_large {
            image.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "avatar_default_big"))
        }
     
        return image
    }()
    
    
    //欢迎信息
    private var welComeLabel: UILabel = {
        let label = UILabel()
        if let name = CZUserCountViewModel.sharedUserAccount.userAcount?.name {
                label.text = name + "欢迎归来"
        }else{
            label.text = "欢迎回来"
        }
        label.font = UIFont.systemFontOfSize(14)
        
        return label
    }()
    
    override func loadView() {
        view = bgImageView
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)
        startAnimation()
    }
    
    private func setUpUI(){
        view .addSubview(iconImage)
        view.addSubview(welComeLabel)
        iconImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        welComeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(10)
            make.centerX.equalTo(iconImage)
        }
 
    }
    
    
    private func startAnimation() {
        self.iconImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
        }
        welComeLabel.alpha = 0
        //阻尼
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
                self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                        self.welComeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //进入首页
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: nil)
                })
        }
        
    
    
    }
    
}
